﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DbControl;
using Common;
using Models;
namespace CentralMonitoring
{
    public partial class DeviceByOilParaSet : Form
    {
        /// <summary>
        /// 油品参数管理窗体
        /// </summary>
      
        public DeviceByOilParaSet()
        {
            InitializeComponent();
        }

        private void DeviceByOilParaSet_Load(object sender, EventArgs e)
        {
            StatusLabel0.Text = string.Format("系统时间：{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            InitCombox();
        }
        private void InitCombox()
        {
            comboBoxPara.DisplayMember = "parameter_name";
            comboBoxPara.ValueMember = "id";
            comboBoxPara.DataSource = OprParameter.GetParameterAll();
            
            if(comboBoxPara.Items.Count > 0)
                comboBoxPara.SelectedIndex = 0;
            RefreshGrid("");
        }
        private void RefreshGrid(string para)
        {           
            DataTable dt = OprRules.GetRuleByCode(para);
            if (dt != null)
            {
                dataGridView1.DataSource = dt; 
                dataGridView1.Columns["Id"].Visible = false;
                dataGridView1.Columns["sensor_id"].Visible = false;
                dataGridView1.Columns["parameter_id"].Visible = false;
                dataGridView1.Columns["rule_code"].HeaderText = "规则名称";
                dataGridView1.Columns["parameter_name"].HeaderText = "指标名称";
                dataGridView1.Columns["upper_upper_value"].HeaderText = "上上限";                
                dataGridView1.Columns["upper_value"].HeaderText = "上限";
                dataGridView1.Columns["lower_value"].HeaderText = "下限";
                dataGridView1.Columns["lower_lower_value"].HeaderText = "下下限";
                dataGridView1.Columns["Flag"].HeaderText = "状态";
                dgSetView.SetFormatDataGridView(dataGridView1);
            }

        }
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e == null || e.Value == null || !(sender is DataGridView))
            {
                return;
            }
            DataGridView dgv = (DataGridView)sender;
            object originalValue = e.Value;
            int dValue = 0;
            if (dgv.Columns[e.ColumnIndex].DataPropertyName == "flag" )
            {
                if (e.Value != null && int.TryParse(e.Value.ToString(), out dValue))
                    e.Value = dValue > 0 ? "启用" : "禁用";
            }
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            RuleParamAdd dlg = new RuleParamAdd();
            dlg.mType = OprType.Add;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                InitCombox();
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int nID = (int)dataGridView1.SelectedRows[0].Cells["id"].Value;//(int)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["Id"].Value;               
                rules dp =OprRules.GetRuleById(nID);
                RuleParamAdd dlg = new RuleParamAdd();
                dlg.mType = OprType.Modify;
                dlg.mPara = dp;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    RefreshGrid("");
                }
            }
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                try
                {
                    if (MessageBox.Show("确定要删除该条数据吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                        return;
                    //int currenID = dataGridView1.SelectedRows.Count;                   
                    //int nId = (int)dataGridView1.Rows[dataGridView1.SelectedRows[currenID].Index].Cells["id"].Value;
                    int nId = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                    if (OprRules.DeleteById(nId) > 0)
                        InitCombox();
                    else
                        MessageBox.Show("删除失败！");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除失败！");
                    lLogWrite.WriteLog(ex.ToString());
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            StatusLabel0.Text = string.Format("系统时间：{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
           
            string para=comboBoxPara.Text;
            RefreshGrid(para);
        }
    }
}
